package com.beizhen.demo.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.beizhen.demo.entity.StuCourse;
import com.beizhen.demo.service.StuCourseService;
import com.beizhen.demo.utils.Fileupload;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
*  前端控制器   王佳乐
*/
    @Controller
    @RequestMapping("/stuCourse")
    public class StuCourseController {
    @Autowired
    private StuCourseService stuCourseService;

    @RequestMapping("/getStuCourseById/{coId}")
    @ResponseBody
    public StuCourse getStuCourseById( @PathVariable("coId") Integer coId){
        StuCourse stuCourse=stuCourseService.getStuCourseById(coId);
        System.out.println("课程id"+stuCourse.getCoId()+"  课程标题:"+stuCourse.getCoTheme()+"  课程摘要:"+stuCourse.getCoDigest()+"  作者:"+stuCourse.getCoAuthor());
        return stuCourse;
    }
    //根据基地id,查询基地课程   王佳乐
    @RequestMapping("/getStuCourseList/{coBid}")
    public String getStuCourseList(@PathVariable("coBid") Integer coBid , ModelMap map){
        List<StuCourse> stuCourseList=stuCourseService.getStuCourseList(coBid);
        for (StuCourse stu : stuCourseList ) {
            System.out.println("基地id"+stu.getCoBid()+"课程id"+stu.getCoId()+"  课程标题:"+stu.getCoTheme()+"  课程摘要:"+stu.getCoDigest()+"  作者:"+stu.getCoAuthor()+" 图片路径:"+stu.getCoCoverimg()+" 发布状态:"+stu.getCoPublishStatic() );
        }
        map.addAttribute("stuCourseList",stuCourseList);
        return "base_administration_curriculum";
    }
    //基地添加课程  王佳乐
    @RequestMapping(value = "/insertStuCourse",method = RequestMethod.POST)
    @ResponseBody
    public String  insertStuCourse(StuCourse stu,HttpSession session, HttpServletRequest request, @RequestParam(value = "CoverImg",required = false) MultipartFile multipartFile){
        //封面url
        String CoverImgPath="";
        //错误信息
        String errorInfo="";
        if(!multipartFile.isEmpty()){
            errorInfo="uploadFileError";
        }
        //上传前的准备工作
        //1、定义上传的目标路径
        String path=session.getServletContext().getRealPath("static"+ File.separator+"uploadfile");
        //2、获取源文件名
        String oldname=multipartFile.getOriginalFilename();
        //3、文件的后缀
        String suffix= FilenameUtils.getExtension(oldname);
        if(multipartFile.getSize()>5000000){
            request.setAttribute(errorInfo,"文件的大小不能超过5M");
            return "base_enter_audit";
        }else if(suffix.equalsIgnoreCase("jpg")||
                suffix.equalsIgnoreCase("png")||
                suffix.equalsIgnoreCase("jpeg")||
                suffix.equalsIgnoreCase("pneg")){
        //写入
        try {
             CoverImgPath= Fileupload.getUrl(multipartFile);
             stu.setCoCoverimg(CoverImgPath);
        } catch (IOException e) {
            e.printStackTrace();
            request.setAttribute(errorInfo,"上传失败");
            return "base_enter_audit";
        }
        }else {
            request.setAttribute(errorInfo, "上传失败");
            return "base_enter_audit";
        }
    /*  StuCourse stu=new StuCourse(1003,"传统文化教育","1.jpg",
                "弘扬传统文化,学习传统文化", 2,"主题","内容",
                "标题","小样");*/
        System.out.println("封面路径:"+CoverImgPath);
        if(stuCourseService.insertStuCourse(stu)>0){
            return "base_administration_curriculum";
        }else{
            return "base_administration_curriculum_particulars";
        }
    }
    @RequestMapping("/upDateStuCourse")
    @ResponseBody
    public String  upDateStuCourse(StuCourse stu,HttpSession session, HttpServletRequest request, @RequestParam(value = "CoverImg",required = false) MultipartFile multipartFile){
        /*StuCourse stu=new StuCourse();
       */
        UpdateWrapper<StuCourse> wrapper=new UpdateWrapper<StuCourse>();
        //取到页面课程对象id,进行判断
        wrapper.eq("co_id",stu.getCoId());
        //封面url
        String CoverImgPath="";
        //错误信息
        String errorInfo="";
        if(!multipartFile.isEmpty()){
            errorInfo="uploadFileError";
        }
        //上传前的准备工作
        //1、定义上传的目标路径
        String path=session.getServletContext().getRealPath("static"+ File.separator+"uploadfile");
        //2、获取源文件名
        String oldname=multipartFile.getOriginalFilename();
        //3、文件的后缀
        String suffix= FilenameUtils.getExtension(oldname);
        if(multipartFile.getSize()>5000000){
            request.setAttribute(errorInfo,"文件的大小不能超过5M");
            return "base_enter_audit";
        }else if(suffix.equalsIgnoreCase("jpg")||
                suffix.equalsIgnoreCase("png")||
                suffix.equalsIgnoreCase("jpeg")||
                suffix.equalsIgnoreCase("pneg")){
        //写入
        try {
             CoverImgPath=Fileupload.getUrl(multipartFile);
             stu.setCoCoverimg(CoverImgPath);
        } catch (IOException e) {
            e.printStackTrace();
            request.setAttribute(errorInfo,"上传失败");
            return "base_enter_audit";
        }
        }else {
            request.setAttribute(errorInfo, "上传失败");
            return "base_enter_audit";
        }
    /*  StuCourse stu=new StuCourse(1003,"传统文化教育","1.jpg",
                "弘扬传统文化,学习传统文化", 2,"主题","内容",
                "标题","小样");*/
        System.out.println("封面路径:"+CoverImgPath);
        if(stuCourseService.upDateStuCourse(stu,wrapper)>0){
            return "base_administration_curriculum";
        }else{
            return "base_administration_curriculum_particulars";
        }

    }
    @RequestMapping("/deleteStuCourse/{coId}")
    public String  DeleteStuCourse(@PathVariable("coId")Integer coId){
        int result=stuCourseService.deleteStuCourseById(coId);
        return "ttt";
    }

}
